#-------------------------------------------------------------------------------
# Name:        ImageHandler
# Purpose:
#
# Author:      Pablo
#
# Created:     12/01/2013
# Copyright:   (c) Monxcleyr Productions 2013
# Licence:     GPL v3
#-------------------------------------------------------------------------------
#!/usr/bin/env python

import pygame, os

class ImageHandler():

  """This class holds all the sprites used by the World and Object classes"""

  def __init__(self):
    self.sprites = {}
    self.root = 'resources/graphics/sprites'


  def getSprite(self, spritename):

    """ Returns the pygame.surface of the requested image. Use in place of loading a sprite
    inside of an object. """

    # Here the imagehandler checks to see if that sprite has already been loaded and is in
    # the self.sprite dictionary. If it is not, it will load that image and put it into a
    # new dictionary entry.

    if spritename not in self.sprites:
      self.sprites[spritename] = pygame.image.load(os.path.join(self.root, spritename))
##      self.sprites[spritename] = self.sprites[spritename].convert()
      if spritename[-4:].lower() == '.bmp':
        self.sprites[spritename].set_colorkey((255,0,255))
      elif spritename[-4:].lower() == '.png':
        self.sprites[spritename] = self.sprites[spritename].convert_alpha()

    return self.sprites[spritename]

  def getImage(self, spritename):

    """ Same as getSprite, but doesn't include root """
    if spritename not in self.sprites:
      self.sprites[spritename] = pygame.image.load(spritename)
##      self.sprites[spritename] = self.sprites[spritename].convert()
      if spritename[-4:].lower() == '.bmp':
        self.sprites[spritename].set_colorkey((255,0,255))
      elif spritename[-4:].lower() == '.png':
        self.sprites[spritename] = self.sprites[spritename].convert_alpha()

    return self.sprites[spritename]


  def reset(self):
    """ Empties the self.sprites dictionary """
    self.sprites = {}
